﻿Imports System.Drawing.Drawing2D
Imports System.ComponentModel
Imports FountainHead.Controls
Imports FountainHead.Model

Public Class Wire
    Implements INotifyPropertyChanged

    Public Sub New(ByVal ctrl As BaseControl)
        controlOneValue = ctrl
    End Sub

#Region "Property"
    Private pathValue As GraphicsPath
    Public ReadOnly Property Path() As GraphicsPath
        Get
            pathValue = New GraphicsPath()
            pathValue.AddLine(TerminalOne.Point, TerminalTwo.Point)
            Return pathValue
        End Get
    End Property

    Private controlOneValue As BaseControl
    Public ReadOnly Property ControlOne As BaseControl
        Get
            Return controlOneValue
        End Get
    End Property

    Private controlTwoValue As BaseControl
    Public ReadOnly Property ControlTwo As BaseControl
        Get
            Return controlTwoValue
        End Get
    End Property

    Private terminalOneValue As Terminal
    Public Property TerminalOne() As Terminal
        Get
            Return terminalOneValue
        End Get
        Set(ByVal value As Terminal)
            terminalOneValue = value
            'AddHandler terminalOneValue.PropertyChanged, AddressOf PropertyChangedHandler
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("Point"))
        End Set
    End Property

    Private terminalTwoValue As Terminal
    Public Property TerminalTwo() As Terminal
        Get
            Return terminalTwoValue
        End Get
        Set(ByVal value As Terminal)
                If TerminalOne.isInputTerminal = value.isInputTerminal Then Throw New Exception("Wire can connect only output and input terminal")
                terminalTwoValue = value
                'AddHandler terminalTwoValue.PropertyChanged, AddressOf PropertyChangedHandler
                RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("Point"))
        End Set
    End Property
#End Region
    Public Event PropertyChanged(ByVal sender As Object, ByVal e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged

    Private Sub PropertyChangedHandler(ByVal sender As Object, ByVal e As PropertyChangedEventArgs)
        Select Case e.PropertyName
            Case "Point"
                RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("Point"))
        End Select
    End Sub

    Public Sub StartWire(ByVal ctrl As BaseControl, ByVal pt As Point)
        TerminalOne = ctrl.NearestTerminal(pt)
    End Sub

    Public Sub FinishedWire(ByVal ctrl As BaseControl, ByVal t As Terminal)
        controlTwoValue = ctrl
        TerminalTwo = t
        If TerminalOne.isInputTerminal Then
            TerminalOne.Current = TerminalTwo.Current
            AddHandler TerminalTwo.PropertyChanged, Sub() TerminalOne.Current = TerminalTwo.Current
        Else
            TerminalTwo.Current = TerminalOne.Current
            AddHandler TerminalOne.PropertyChanged, Sub() TerminalTwo.Current = TerminalOne.Current

        End If
    End Sub

End Class